package com.nh.utils.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * HttpConnection GET/SET 请求
 * @author tdw
 *
 */
public class HttpConnection {
  
  private static int connectTimeout=2000;
  private static int readTimeout = 2000;
  
   
  public static int getConnectTimeout() {
   return connectTimeout;
  }
  /**
   * 设置请求超时时间，默认为2000ms
   * @param connectTimeout
   */
  public static void setConnectTimeout(int connectTimeout) {
   HttpConnection.connectTimeout = connectTimeout;
  }

  public static int getReadTimeout() {
   return readTimeout;
  }
  /**
   * 设置请求处理超时时间，默认为2000ms
   * @param readTimeout
   */
  public static void setReadTimeout(int readTimeout) {
   HttpConnection.readTimeout = readTimeout;
  }

  /**
   * 
   * @param requestUrl 请求URL地址
   * @param encoding 结果编码处理
   * @return String
   */
  public static String doPostRequest(String requestUrl,String encoding) {
   HttpURLConnection urlConnection = null;
   StringBuffer buffer = new StringBuffer();
   try {
    URL url = new URL(requestUrl);
    urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("POST");
    urlConnection.setConnectTimeout(getConnectTimeout());
    urlConnection.setReadTimeout(getReadTimeout());
    urlConnection.connect();
    InputStream in = urlConnection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(in,encoding));
    String line = null;
    while ((line = rd.readLine()) != null) {
     buffer.append(line);
    }
    rd.close();
    in.close();
   } catch (Exception e) {
//     logger.error(reqUrl+"请求失败:",e);
    e.printStackTrace();
   } finally {
    if (urlConnection != null) {
     urlConnection.disconnect();
    }
   }
   return buffer.toString();
  }
  
  
  /**
   * 像目标地址发送GET请求
   * @param requestUrl 请求URL地址
   * @param encoding 结果编码处理
   * @return String
   */
  public static String doGetRequest(String requestUrl,String encoding){
   StringBuffer buffer = new StringBuffer();
   HttpURLConnection urlConnection = null;
   try {
    URL url = new URL(requestUrl);
    urlConnection = (HttpURLConnection)url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setConnectTimeout(getConnectTimeout());
    urlConnection.setReadTimeout(getReadTimeout());
    urlConnection.connect();
    BufferedReader read = new BufferedReader(new InputStreamReader(
      urlConnection.getInputStream(),encoding));
    String str = "";
    while((str=read.readLine())!=null){
     buffer.append(str);
    }
    read.close();
   } catch (MalformedURLException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }finally {
    if (urlConnection != null) {
     urlConnection.disconnect();
    }
   }
   return buffer.toString();
  }
  
  
}